package com.yunti.springboot.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunti.springboot.mapper.SubItemMapper;
import com.yunti.springboot.po.SubItem;
import com.yunti.springboot.service.SubItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunti.springboot.vo.Result;
import com.yunti.springboot.vo.req.SubItemReq;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ytt
 * @since 2023-12-13
 */
@Service
public class SubItemServiceImpl extends ServiceImpl<SubItemMapper, SubItem> implements SubItemService {


    @Override
    public Result findPage(SubItemReq req) {
        if (req.getCurrent() == null) {
            req.setCurrent(1);
        }
        if (req.getSize() == null) {
            req.setSize(10);
        }
        Page<SubItem> page = new Page<SubItem>(req.getCurrent() , req.getSize());
        LambdaQueryWrapper<SubItem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Objects.nonNull(req.getScoreType()),SubItem::getScoreType, req.getScoreType());
        lambdaQueryWrapper.eq(Objects.nonNull(req.getUnit()),SubItem::getUnit, req.getUnit());
        lambdaQueryWrapper.eq(SubItem::getSubId, req.getSubId());
        lambdaQueryWrapper.eq(Objects.nonNull(req.getSextype()),SubItem::getSextype, req.getSextype());
        lambdaQueryWrapper.like(StrUtil.isNotBlank(req.getName()),SubItem::getName, req.getName());
        return Result.success(baseMapper.selectPage(page,lambdaQueryWrapper));
    }

    @Override
    public Result add(SubItemReq req) {
        LambdaQueryWrapper<SubItem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SubItem::getSubId, req.getSubId());
        lambdaQueryWrapper.eq(SubItem::getItemNo, req.getItemNo());
        lambdaQueryWrapper.ne(SubItem::getId, req.getId());
        if(baseMapper.selectList(lambdaQueryWrapper).size()>0){
            return Result.error("添加失败,小项号存在");
        }
        SubItem item = new SubItem();
        BeanUtils.copyProperties(req,item);
        if(baseMapper.insert(item)>0){
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }

    @Override
    public Result edit(SubItemReq req) {
        LambdaQueryWrapper<SubItem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SubItem::getSubId, req.getSubId());
        lambdaQueryWrapper.eq(SubItem::getItemNo, req.getItemNo());
        lambdaQueryWrapper.ne(SubItem::getId, req.getId());
        if(baseMapper.selectList(lambdaQueryWrapper).size()>0){
            return Result.error("更新失败,小项号存在");
        }
        SubItem item = new SubItem();
        BeanUtils.copyProperties(req,item);
        if(baseMapper.updateById(item)>0){
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }

    @Override
    public Result del(SubItemReq req) {
        if(baseMapper.deleteById(req.getId())>0){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }
}
